<template>
  <div class="content">
    <div class="header">
      <h3>{{ contentConfig.header?.title }}</h3>
      <el-button v-if="isCreate" type="primary" @click="foundUser">{{
        contentConfig.header?.btnTitle
      }}</el-button>
    </div>
    <!-- 表格 -->
    <div class="table">
      <el-table :data="pageList" border style="width: 100%" v-bind="contentConfig.childrenTree">
        <template v-for="item in contentConfig.contentData" :key="item.prop">
          <template v-if="item.type === 'timer'">
            <el-table-column align="center" v-bind="item">
              <template #default="scope"> {{ formatDay(scope.row[item.prop]) }} </template>
            </el-table-column>
          </template>
          <template v-else-if="item.type === 'handler'">
            <el-table-column align="center" v-bind="item">
              <template #default="scope">
                <el-button
                  v-if="isUpdate"
                  type="primary"
                  icon="Edit"
                  size="small"
                  @click="editUserInfo(scope.row)"
                ></el-button>
                <el-button
                  v-if="isDelete"
                  type="danger"
                  icon="Delete"
                  size="small"
                  @click="deleteUser(scope.row)"
                ></el-button>
              </template>
            </el-table-column>
          </template>
          <template v-else-if="item.type === 'icon'">
            <el-table-column v-bind="item" align="center">
              <template #default="scope">
                <el-icon size="large">
                  <component
                    :is="scope.row.icon ? scope.row.icon.split('el-icon-')[1] : false"
                  ></component>
                </el-icon>
              </template>
            </el-table-column>
          </template>
          <template v-else>
            <el-table-column align="center" v-bind="item"> </el-table-column>
          </template>
        </template>
      </el-table>
    </div>
    <!-- 分页器 -->
    <div class="page">
      <el-pagination
        v-model:current-page="currentPage"
        background
        v-model:page-size="pageSize"
        :page-sizes="[5, 10, 15]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageTotalCount"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ElMessage, ElMessageBox } from 'element-plus'
import useSystemStore from '@/store/modules/system'
import { storeToRefs } from 'pinia'
import { formatDay } from '@/utils/format'
import { ref } from 'vue'
import useMenuAuthor from '@/hooks/useMenuAuthor'
// import type { querListType } from '../type'
interface Iprops {
  contentConfigCom?: string[]
  contentConfig: {
    pageName: string
    header?: {
      title?: string
      btnTitle?: string
    }
    contentData: any[]
    childrenTree?: any
  }
}
const props = defineProps<Iprops>()
// 发起action，请求userList的数据
const systemStore = useSystemStore()

// 控制菜单的权限
const isCreate = useMenuAuthor(`${props.contentConfig.pageName}:create`)
const isDelete = useMenuAuthor(`${props.contentConfig.pageName}:delete`)
const isUpdate = useMenuAuthor(`${props.contentConfig.pageName}:update`)
const isQuery = useMenuAuthor(`${props.contentConfig.pageName}:query`)

const emit = defineEmits(['handlerNewClick', 'handlerEditClick'])
const currentPage = ref(1)
const pageSize = ref(10)
// $onAction监听 action 和它们的结果
// after 在 action 返回或解决后的钩子
systemStore.$onAction(({ name, after }) => {
  after(() => {
    if (name === 'deleteDataInfo' || name === 'pageNewDataInfo' || name === 'pageEditUserInfo') {
      currentPage.value = 1
    }
  })
})

// 因为是异步调用所以页面一刷新的时候可能是空的数据 所以需要 storeToRefs来变成响应式
// 当数据改变的时候  上面使用的数据就会自动刷新
postUserList()
const { pageList, pageTotalCount } = storeToRefs(systemStore)

function handleSizeChange() {
  postUserList()
}
function handleCurrentChange() {
  postUserList()
}
// 封装一个函数方便点击页码的时候调用
function postUserList(queryList: any = {}) {
  if (!isQuery) return
  const size = pageSize.value
  const offset = (currentPage.value - 1) * size
  const Info = { size, offset }
  systemStore.pageListInfo(props.contentConfig.pageName, { ...Info, ...queryList })
}
// 删除一个用户
function deleteUser(row: any) {
  ElMessageBox.confirm('确认删除吗', 'Warning', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
    closeOnClickModal: false
  })
    .then(() => {
      systemStore.deleteDataInfo(props.contentConfig.pageName, row.id)
      ElMessage({
        type: 'success',
        message: '删除成功'
      })
    })
    .catch(() => {
      ElMessage({
        type: 'success',
        message: '取消成功'
      })
    })
}
// 新建用户
function foundUser() {
  emit('handlerNewClick')
}
// 修改用户
function editUserInfo(userInfo: any) {
  emit('handlerEditClick', userInfo)
}

// 向父组件暴露出一个方法
defineExpose({
  postUserList
})
</script>

<style lang="less" scoped>
.content {
  padding: 20px;
  background-color: var(--el--page--search--bg--);
  border: 1px solid var(--el-border-color);
  border-radius: 10px;
  margin-top: 20px;
}
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.table {
  margin-top: 20px;
  .el-table .el-table__cell {
    padding: 5px 0;
  }
  .el-table {
    z-index: 0;
    :deep(.cell) {
      // font-size: 12px;
      min-width: 0;
      overflow: hidden;
      box-sizing: border-box;
      text-overflow: ellipsis;
      white-space: nowrap;
      word-break: break-all;
    }
  }
}

.page {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}
.el-overlay {
  :global(.el-overlay-message-box) {
    z-index: 99999;
    height: 100%;
    width: 100%;
    background: rgba(0, 0, 0, 0.2);
  }
}
</style>
